package com.tr.rentcar.model.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.UserAddress;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ParamHandler;
import com.tr.rentcar.model.jdbc.ResultHandler;
/**
 * 租车人信息
 * @author lenovo
 *
 */
public class UserAddressDao implements ResultHandler<UserAddress>{
	private MySqlDb db= MySqlDb.getInstance();
	/*
	 * 通过电话查找
	 */
	public UserAddress findbytel(String tel) throws SQLException {
		String sql = "select * from user_address  where uaTel = ?;";
		return db.queryUninResult(sql, new GlobalParamHandler(tel),this);
	}
	/**
	 * 添加一条记录
	 */
	public boolean save( UserAddress us) throws SQLException {
		String sql = "insert into user_address(ua_name,ua_tel,ua_identity,uid) values(?,?,?,?)";
		return db.executeUpdate(sql, new SaveParamHandler(us)) > 0;
	}
	public boolean deleteByuaid(int uaid) throws SQLException {
		String sql = "delete from user_address  where ua_id = ?;";
		return db.executeUpdate(sql, new GlobalParamHandler(uaid)) > 0;
	}
	/**
	 * 查寻所有有
	 */
	public List<UserAddress> findAll() throws SQLException {
		String sql = "select * from  user_address";
		return db.executeQuery(sql,this);
	}
	/**
	 * 根据用户id查寻
	 */
	public List<UserAddress> findById(int uid) throws SQLException {
		String sql = "select * from user_address where uid = ? order by ua_id desc";
		return db.executeQuery(sql, new GlobalParamHandler(uid), this);
	}
	/**
	 * 通过用户ID查询记录数
	 */
	public long getAllCount(int uid) throws SQLException{
		String sql="select count(*)as count from user_address where uid=?";
		return (long) db.executeCountQuery(sql,new GlobalParamHandler(uid));
	}
	/**
	 * 通过uaid修改租车信息
	 */
	public Boolean xgByUaid(int uaid,String uaName,String uaTel,String uaIdentity) throws SQLException {
		String sql = "update user_address set ua_name = ?,ua_tel=?,ua_identity=? where ua_id = ?";
		return db.executeUpdate(sql, new GlobalParamHandler(uaName,uaTel,uaIdentity,uaid)) > 0;
	}
	@Override
	public UserAddress doHander(Map<String, Object> row) {
		UserAddress ua=new UserAddress();
		ua.setUaId(Integer.parseInt( row.get("ua_id")+""));
		ua.setUaName(row.get("ua_name")+"");
		ua.setUaTel(row.get("ua_tel")+"");
		//将身份证号码中间隐藏
		ua.setUaIdentity(row.get("ua_identity")+"");
		ua.setUid(Integer.parseInt( row.get("uid")+""));
		return ua;
	}
	private class SaveParamHandler implements ParamHandler {
		UserAddress user;

		SaveParamHandler(UserAddress us) {
			this.user = us;
		}

		@Override
		public void doHander(PreparedStatement pStatement) throws SQLException {
			// 处理sql参数
			pStatement.setString(1, user.getUaName());
			pStatement.setString(2, user.getUaTel());
			pStatement.setString(3, user.getUaIdentity());
			pStatement.setInt(4, user.getUid());

		}
	}
}
